/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦]
 * 
 * https://zhiqim.org/project/zhiqim_components/zhiqim_admin.htm
 *
 * Zhiqim Admin is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.manager;

import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.ZmlContexts;
import org.zhiqim.httpd.context.core.Interceptor;
import org.zhiqim.kernel.annotation.AnAlias;
import org.zhiqim.kernel.util.Strings;
import org.zhiqim.manager.dao.ZmrParamDao;

/**
 * 检查是否登录拦截器
 *
 * @version v1.0.0 @author zouzhigang 2015-5-28 新建与整理
 */
@AnAlias("chkZmrLogin")
public class ZmrInterceptor implements Interceptor, ZmrConstants
{
    public void intercept(HttpRequest request) throws Exception
    {
        if (request.isXMLHttpRequest())
        {//AJAX访问
            
            if (!request.hasSessionUser(ZmrSessionUser.class))
            {
                request.setResponseError("您闲置太久或未登录，请重新登录后再操作...");
                return;
            }
        }
        else
        {//URL访问
            
            ZmrSessionUser sessionUser = request.getSessionUser(ZmrSessionUser.class);
            if (sessionUser == null)
            {
                request.setRedirectTop("/"+request.getContextAttributeString(ZMR_PATH)+"/logout.htm", "您闲置太久或未登录，正在返回初始界面，请确定.....");
                return;
            }
            
            if (ZmrParamDao.isThemeFrame() && _GET_.equalsIgnoreCase(request.getMethod()) && 
                (Strings.endsWith(request.getPathInContext(), ".htm") || Strings.endsWith(request.getPathInContext(), ".zml")) &&
                !ZmlContexts.parseZmlContent(request, ZMR_MAIN_URL_DEFAULT).equals(request.getPathInContext()) && !ZMR_TEMPLATE_INNER.equals(request.getView()))
            {
                //如果是GET且访问页，结尾是.htm/.zml，且不是主页和左页的，则设置为当前页为右部页，方便刷新是指向
                sessionUser.setMainUrl(request.getRequestURI());
            }
        }
    }
}
